package ua.pp.shell.wka;

import ua.pp.shell.wka.DBAdapter.Types.Vocabulary;
import ua.pp.shell.wka.DBAdapter.Types.Word;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class WordListActivity extends Activity {
	private boolean reversed = false;
    private DBAdapter dbAdapter = null;
    public Vocabulary vocabulary = null;
    public int on_page = 20;
    public class Components {
    	TextView vocabulary_title = null;
    	ImageButton filter_button = null;
    	ListView list = null;
    	EditText filter_edit = null;
    	TextView page_label = null;
		TextView pages_label = null;
		ImageButton page_back_button = null;
		ImageButton page_forward_button = null;
		Button add_word_button = null;
    	
    	public Components() {
    		vocabulary_title = (TextView)findViewById(R.id.vocabulary_title);
    		filter_button = (ImageButton)findViewById(R.id.filter);
    		list = (ListView)findViewById(R.id.list);
    		filter_edit = (EditText)findViewById(R.id.filter_query);
    		page_label = (TextView)findViewById(R.id.page);
    		pages_label = (TextView)findViewById(R.id.pages);
    		page_back_button = (ImageButton)findViewById(R.id.page_back);
    		page_forward_button = (ImageButton)findViewById(R.id.page_forward);
    		add_word_button = (Button)findViewById(R.id.add_word);
    	}
    };
    
    public Components c = null;
    public class WordListPager extends Pager {
    	public WordListPager(int on_page) {
			super(on_page);
		}

		@Override
    	public int GetCount() {
    		String title_filter = c.filter_edit.getText().toString();
    		int count = dbAdapter.CountWords(vocabulary.id, title_filter);
    		this.Fill(count, this.on_page);
    		c.page_label.setText(Integer.toString(this.page + 1));
    		c.pages_label.setText(Integer.toString(this.pages));
    		if (page > 0) {
    			c.page_back_button.setClickable(true);
    		} else {
    			c.page_back_button.setClickable(false);
    		}
    		
    		if (page + 1 < pages) {
    			c.page_forward_button.setClickable(true);
    		} else {
    			c.page_forward_button.setClickable(false);
    		}
			return count;
    	}
    }
    private Pager pager = null;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = this.getIntent();
        Settings settings = new Settings(getApplicationContext());
        int id = intent.getIntExtra("id", settings.GetCurrentVocabulary());
        if (id < 0) {
        	finish();
        }
        dbAdapter = new DBAdapter(this);
        vocabulary = dbAdapter.GetVocabulary(id);
        setContentView(R.layout.words_activity);
        c = new Components();
        pager = new WordListPager(this.on_page);
        c.vocabulary_title.setText(getString(R.string.vocabulary)+": "+vocabulary.title);
        c.filter_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				pager.page = 0;
				UpdateListAdapter();
			}
		});
        
        c.page_back_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				int page = pager.page;
				pager.SetPage(page - 1);
				UpdateListAdapter();
			}
		});
        
        c.page_forward_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				int page = pager.page;
				pager.SetPage(page + 1);
				UpdateListAdapter();
			}
		});
        c.add_word_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(getApplicationContext(), AEWordActivity.class);
        		intent.putExtra("id_vocabulary", vocabulary.id);
	        	startActivityForResult(intent, App.activities.AE_WORD.ordinal());
			}
		});
        //At the end because of reassigning
        UpdateListAdapter();
    }
    
    public void UpdateListAdapter() {
    	EditText filter = (EditText)findViewById(R.id.filter_query);
    	String title_filter = filter.getText().toString();
    	pager.ReFill();
        WordListAdapter listAdapter = new WordListAdapter(this, 1, dbAdapter.GetWords(vocabulary.id, title_filter, pager.from, pager.on_page));
        listAdapter.reversed = reversed;
        c.list.setAdapter(listAdapter);
    }
        
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.wordlist, menu);
        return true;
    }
    
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) {
    	Intent intent;
    	switch (item.getItemId()) {
    		case R.id.reverse:
    			reversed = !reversed;
    			UpdateListAdapter();
    		break;
    		case R.id.teach:
    			intent = new Intent(getApplicationContext(), TeacherActivity.class);
        		intent.putExtra("id", vocabulary.id);
	        	startActivityForResult(intent, App.activities.TEACH.ordinal());
    		break;
        	case R.id.add:
        		intent = new Intent(getApplicationContext(), AEWordActivity.class);
        		intent.putExtra("id_vocabulary", vocabulary.id);
	        	startActivityForResult(intent, App.activities.AE_WORD.ordinal());
	        break;
        	case R.id.remove:
        		AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
                alertbox.setMessage(R.string.delete_confirmation);
                alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface arg0, int arg1) {
                    	GetListAdapter().DeleteSelected();
                    	pager.page = 0;
                     	UpdateListAdapter();
                    }
                });
                alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface arg0, int arg1) {
                        Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show();
                    }
                });
                alertbox.show();
        		break;
        	case R.id.quit:
        		this.finish();
        		break;
    	}
    	return true;
    }
    
    public WordListAdapter GetListAdapter() {
    	ListView list = (ListView)findViewById(R.id.list);
		return (WordListAdapter)list.getAdapter();
    }
    
    protected void onActivityResult(int request, int result, Intent data) {
    	switch(result) {
    		case RESULT_OK:
    			if (App.activities.AE_WORD.ordinal() == request) {
    				int id = data.getIntExtra("id", 0);
        			boolean add_new = data.getBooleanExtra("add_new", false);
        			Word word = dbAdapter.GetWord(id);
                    if ( add_new == false ) {
                    	GetListAdapter().UpdateWord(word);
                    	Toast.makeText(this, String.format(getString(R.string.word_updated), word.word), 2000).show();
                    } else if ( add_new == true ) {
                    	GetListAdapter().AddWord(word);
                    	Toast.makeText(this, String.format(getString(R.string.word_created), word.word), 2000).show();
                    }   
                    UpdateListAdapter();
    			}
    			break;
    		case RESULT_CANCELED:
    			switch (request) {
    				case 2:
    					String message = data.getStringExtra("message");
    					Toast.makeText(this, message, 2000).show();
    					break;
    			}
    			break;
    	}
    }
}